﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Utility.Other
{
    public static class FindMoney
    {
        /// <summary>
        /// szuka wartosci pieniadza spelniajacego schemat XXX,XX - zwraca maksymalna wartosc
        /// </summary>
        /// <param name="contentWhereSearchForMoney"></param>
        /// <returns></returns>
        public static decimal FindMaxMoneyInString(string contentWhereSearchForMoney)
        {
            decimal maxMoney = 0, temp = 0;

            if (contentWhereSearchForMoney != null && contentWhereSearchForMoney != string.Empty)
            {
                //contentWhereSearchForMoney = contentWhereSearchForMoney.Replace(" ", ""); //zamien 1 255,00 zł na 1255,00zł
                MatchCollection foundedMoney;
                Regex regFindMoney = new Regex(@"\d+,\d{2}", RegexOptions.Compiled);

                foundedMoney = regFindMoney.Matches(contentWhereSearchForMoney);

                //znajdz max
                foreach (Match match in foundedMoney)
                {
                    if (decimal.TryParse(match.Value.Replace(",", "."), out temp))
                    {
                        if (maxMoney < temp) maxMoney = temp;
                    }
                    if (decimal.TryParse(match.Value, out temp))
                    {
                        if (maxMoney < temp) maxMoney = temp;
                    }
                    string test = match.Value;
                    string test2 = match.Value.Replace(",", ".");
                }
            }

            return maxMoney;
        }
    }
}
